package com.study.leetcode;

public class Solution_416 {
    public boolean canPartition(int[] nums) {
        int length = nums.length;
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        if ((sum & 1) == sum) return false;
        int half = sum / 2;
        int[][] dp = new int[length + 1][half + 1];
        for (int i = 1; i <= length; i++) {
            for (int j = 1; j <= half; j++) {
                if (j >= nums[i - 1]) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i - 1]] + nums[i - 1]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return (sum - dp[length][half]) == 0;
    }
}
